
from numpy import *
from scipy.integrate import quad


class Math:
    
    def extremums(x, y, e=0.0001):
	lx = len(x)
	xx = []
	yy = []
	xx.append(x[0])
	yy.append(y[0])
	for i in range(1, lx-1):
	    if y[i-1] == y[i] and y[i] == y[i+1]:
		continue
	    if y[i-1] <= y[i] and y[i] >= y[i+1]: # /\
		xx.append(x[i])
		yy.append(y[i])
		continue
	    if y[i-1] >= y[i] and y[i] <= y[i+1]: # \/
		xx.append(x[i])
		yy.append(y[i])
		continue
	    if abs((y[i+1] - y[i])/(x[i+1] - x[i]) - (y[i] - y[i-1])/(x[i] - x[i-1])) > e:
		xx.append(x[i])
		yy.append(y[i])
		continue
	xx.append(x[-1])
	yy.append(y[-1])
	
	return {'x': xx, 'y': yy}
    extremums = staticmethod(extremums)
    
    def integral(func, a, b, *args):
	return quad(func, a, b, *args, full_output=1)[0]
    integral = staticmethod(integral)

    def mediana(func, a, b, e=0.0001):
	l = a
	r = b
	while l <= r:
	    k = (l + r)/2
	    left  = Math.integral(func, a, k)
	    right = Math.integral(func, k, b)
	    diff = left - right
	    
	    if abs(diff) < e:
		return k
	    if left < right:
		l = k
	    else: # left > right
		r = k
	return None
    mediana = staticmethod(mediana)
    
    def power(x):
	s = 0
	for i in x:
	    s += i
	return s
    power = staticmethod(power)

    def max_(x):
	s = 0
	for i in x:
	    if i > s:
		s = i
	return s
    max_ = staticmethod(max_)
